package com.parp.visuals;

import java.util.ArrayList;
import java.util.List;
import com.utils.Circle;
import com.utils.Utils;

import android.graphics.Canvas;
import android.graphics.Paint;

public class CircleVisual implements Visual{
	private final List<Circle> circles =  new ArrayList<Circle>();
	private final List<Circle> oldCircles =  new ArrayList<Circle>();
	private final Paint circlePaint = new Paint();
	private boolean enable = true;
	
	public CircleVisual(){
		circlePaint.setStrokeWidth(3);
	}
	
	@Override
	public void enable(boolean enable)
	{
		this.enable = enable;
		synchronized (circles) {
			this.circles.clear();
			this.oldCircles.clear();
		}
	}
	
	@Override
	public void motionUpdate(int pointerId, float x, float y, long time, int height) {
		synchronized (circles)
		{
			if(enable && time > 1) {
				if (circles.size() < 30)
				{
					x = x + (20 - Utils.getRand(40));
					y = y + (20 - Utils.getRand(40));
					int r = 5 + Utils.getRand(20);
					circles.add(new Circle((int)x, (int)y, r));
				}
			}
		}
	}

	@Override
	public void drawVisual(int pointerId, Canvas canvas, float x, float y, long time) {
		synchronized (circles)
		{
			if(enable) {
			for(Circle c : circles)
				{
					circlePaint.setARGB(255, Utils.getRand(255), Utils.getRand(255), Utils.getRand(255));
					canvas.drawCircle(c.getX(), c.getY(), c.getRadius(), circlePaint);
					c.setRadius(c.getRadius() - 1);
					if (c.getRadius() < 1)
					{
						oldCircles.add(c);
					}
				}
				if(oldCircles.size() > 0)
				{
					circles.removeAll(oldCircles);
					oldCircles.clear();
				}
			}
		}
	}

	@Override
	public void up() {
	}
}
